#pragma once
#include<iostream>
#include<pthread.h>
#include <unistd.h>


namespace LockModule
{
    class Mutex
    {
    public:
        //防止锁被拷贝 
        Mutex(const Mutex&) = delete;
        const Mutex& operator = (const Mutex&) = delete;
        Mutex()
        {
            int n = ::pthread_mutex_init(&_lock ,nullptr);
            if(n!= 0) std::cout<<"pthread_mutex_init error"<<std::endl;
        }
        void Lock()
        {
            int n = ::pthread_mutex_lock(&_lock);
            if(n!= 0) std::cout<<"lock error"<<std::endl;

        }
        void Unlock()
        {
            int n =::pthread_mutex_unlock(&_lock);
            if(n!= 0) std::cout<<"unlock error"<<std::endl;

        }
        pthread_mutex_t* LockPtr()
        {
            return &_lock;
        }
        ~Mutex()
        {
            int n = pthread_mutex_destroy(&_lock);
            if(n!= 0) std::cout<<"destroy error"<<std::endl;
            
        }
    private:
        pthread_mutex_t _lock;
    };

    class LockGuard
    {
    public:
        LockGuard(Mutex &mtx):_mtx(mtx)
        {
            _mtx.Lock();
        }
        ~LockGuard()
        {
            _mtx.Unlock();
        }
    private:
        Mutex &_mtx;//注意这里是引用
    };

}